package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 *	5744
 * http://www.wlmqwb.com/  乌鲁木齐在线
 * 	
 * @author KK
 *
 */
/**
 * http://www.xinjiangnet.com.cn/xj/corps/201311/t20131126_3606776.shtml
 */
public class WlmqwbNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		/*
		*		[0-9]*
		*       [a-z]*
		*		[a-z0-9]*
		*       [a-zA-Z0-9]*
		*/		
		
		//"http://www.wlmqwb.com/[a-z0-9]*/[0-9]*/t[0-9]*_[0-9]*.shtml",
		//"http://www.wlmqwb.com/[a-z0-9]*/[a-z0-9]*/[0-9]*/t[0-9]*_[0-9]*.shtml",
		"http://[a-z]*.wlmqwb.com/[0-9]*/[a-z]*/[A-Z0-9]*.html"
	};	
	
	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}		
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();	
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);				
		news.setUrl(url);	
		
		title =doc.select("div.text dt").text();	
//		if (title!=null)title=StringUtils.substringBefore(title, "-");
		content=HtmlCleaner.getContentHtml(url,doc.select("div.news_cont"));	
		/*
		 * 作者
		 */
//		author = JSoupUtils.matchAuthor(doc, "来源：");
		Matcher matcher = Pattern.compile("document.write\\(unescape.*id=([0-9]+)").matcher(htmltxt);
		if(matcher.find()){
			String id = matcher.group(1);
			String dateUrl = "http://x.push.bz/c/shownews/zuozhe.ashx?id="+id;
			UrlMeta dateMeta = CrawlHTML.responseToURL(dateUrl);
			if(dateMeta != null && StringUtils.isNotBlank(dateMeta.getHtml())){
				String datetxt = dateMeta.getHtml();
				author = StringUtils.substringBetween(datetxt, "来源：", "&nbsp;");
				date = DateUtils.matchDate(StringUtils.substringBetween(datetxt, "<span>", "</span>"));
			}
		}
		
//		if (StringUtils.isNotBlank(title) && date!=null){			
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(content);
			news.setAuthor(author);
			news.setDate(date);	
//			news.setType(1);	
//		}
		return news;
	}	
	public static void main(String[] args) {
		String url="http://w.wlmqwb.com/2015/december/38D425A1.html";
		WlmqwbNewsAnalyse analyse=new WlmqwbNewsAnalyse();		
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
